﻿<META http-equiv="refresh" content="<?php echo rand(500,600); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 篮球视频数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

date_default_timezone_set("Asia/Shanghai");
@ $date = $_GET["date"];

if ($date == "") {
	$searchDate = date("Ymd");
	$date = date("Y-m-d");
}

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

$url = "http://www.bbboo.com/nba/video/" . $searchDate . ".html?" . rand();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$HtmlContents = curl_exec($ch);
curl_close($ch);

//先取得当前数据库中的数据
$sql = "select matchTime, videoId from live_video where matchTime = '" . $date . "' and videoCategory = '0'";
$result = mysqli_query($conn, $sql);

$sql = "INSERT INTO live_video (matchTime, videoId, videoTitle, videoImgUrl, videoLength, videoUrl, homeTeam, visitTeam, videoCategory, updateDate) VALUES";

$HtmlContents = str_get_html($HtmlContents);

$teamList = $HtmlContents->find('div[class=area_b]');

$i = 0;
$j = 0;
foreach ($teamList as $team) {
	$homeTeam = null;
	$visitTeam = null;
	$teamName = $team->find('div[class=tl] span', 0)->innertext;
	if (strpos($teamName, "vs")) {

		$teamName = explode("vs", $teamName);
		//取得主队名称
		$homeTeam = $teamName[0];
		//取得客队名称
		$visitTeam = $teamName[1];

		$videoList = $team->find('div[class=video]');
		foreach ($videoList as $video) {

			$id = $video->find('div[class=name] a ', 0)->href;
			$id = substr($id, 28, 12);
			echo "videoid：" . $id;
			echo '<br>';
			$title = preg_replace("/\(.+\)/", "", ($video->find('div[class=name] a', 0)->innertext));
			$title = explode(" ", $title);
			$title = $title[2];
			echo "title：" . $title;
			echo '<br>';
			echo "homeTeam：" . $homeTeam;
			echo '<br>';
			echo "visitTeam：" . $visitTeam;
			echo '<br>';
			$imgUrl = $video->find('div[class=img] img ', 0)->src;
			echo "imgurl：" . $imgUrl;
			echo '<br>';
			$videoLength = $video->find('div[class=length]', 0)->innertext;
			echo "videoLength：" . $videoLength;
			echo '<br>';

			$insert = true;
			mysqli_data_seek($result, 0);
			while ($row_array = mysqli_fetch_array($result, MYSQL_ASSOC)) {
				if ($row_array["videoId"] == $id && $row_array["matchTime"] == $date) {
					$insert = false;
					break;
				}
			}
			if ($insert && $title != "全场下载" && $title != "重播录像") {

				//取得播放地址
				$videoUrl = file_get_contents("http://active.3b2o.com:8090/php/video/ajax.php?action=down&jsonp=v.show_down_true&id=" . $id);
				$videoUrl = str_replace("v.show_down_true(", "", $videoUrl);
				$videoUrl = str_replace(")", "", $videoUrl);
				$videoUrl = json_decode($videoUrl);
				$videoUrl = $videoUrl->downlinks;
				//如果播放地址不存在，则不抓取该数据。
				if (count($videoUrl) == 0) {
					echo '<hr>';
					continue;
				}
				
				$j = $j +1;
				$videoUrl = $videoUrl[0];
				$videoUrl = $videoUrl->url;
				echo "videoUrl：" . $videoUrl;
				echo '<br>';
				$sql .= "('" . $date . "', '" . $id . "', '" . $title . "', '" . $imgUrl . "', '" . $videoLength . "', '" . $videoUrl . "', '" . $homeTeam . "', '" . $visitTeam . "', '0', now()),";
			}
			echo '<hr>';
		}

	}
}
$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

if ($j > 0) {
	$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}

/*
function ping() {
	if (!mysql_ping($this->link)) {
		mysql_close($this->link); //注意：一定要先执行数据库关闭，这是关键
		$this->connect();
	}
}
*/
?>